package com.wzw.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Timer;
import java.util.TimerTask;

public class VisitorCounter{
	
	private static boolean saver_is_started = false;
	
	private final static String FROM_HEXUN = "1";
	private final static String FROM_21CENTURY = "2";
	private final static String FROM_DONGCAI = "3";
	private final static String FROM_HSBCJT = "4";
	private final static String FROM_REDBOY = "5";
	private final static String FROM_HSEDM = "6";
	private final static String FROM_SINA = "7";
	
	
	private static VisitorCounter instance = null;
	private boolean hasRead = false;
	private int count_HEXUN = 0;
	private int count_21CENTURY = 0;
	private int count_DONGCAI = 0;
	private int count_REDBOY = 0;
	private int count_HSBCJT = 0;
	private int count_HSBCJT_EDM = 0;
	private int count_SINA = 0;
	private int count_OTHERS = 0;
	
	private int count_TOHSBCJT = 0;
	
	private VisitorCounter(){}
	
	public synchronized static VisitorCounter getInstance(){
		if(instance==null){
			instance = new VisitorCounter();
		}
		return instance;
	}
	
	public void add(String from){
		if(FROM_HEXUN.equals(from))
			count_HEXUN++;
		else if(FROM_DONGCAI.equals(from))
			count_DONGCAI++;
		else if(FROM_21CENTURY.equals(from))
			count_21CENTURY++;
		else if(FROM_REDBOY.equals(from))
			count_REDBOY++;
		else if(FROM_HSBCJT.equals(from))
			count_HSBCJT++;
		else if(FROM_HSEDM.equals(from))
			count_HSBCJT_EDM++;
		else if(FROM_SINA.equals(from))
			count_SINA++;
		else
			count_OTHERS++;
	}
	
	public void addToHsbcjt(){
		count_TOHSBCJT++;
	}
	
	public void saveCount(String counterFile){
		try {
			System.out.println("Read/Save counter File Begin...");
			//System.out.println(counterFile);
			if(!hasRead){
				BufferedReader br = new BufferedReader(new FileReader(counterFile));
				String line = null;
				while ((line = br.readLine()) != null) {
					int loc = -1;
					if ((loc = line.indexOf("//")) != -1)
						line = line.substring(0, loc).trim();
					try{
						if (!"".equals(line)){
							String[] counts = line.split(",");
							count_HEXUN += Integer.parseInt(counts[0]);
							count_21CENTURY += Integer.parseInt(counts[1]);
							count_DONGCAI += Integer.parseInt(counts[2]);
							count_REDBOY += Integer.parseInt(counts[3]);
							count_HSBCJT += Integer.parseInt(counts[4]);
							count_HSBCJT_EDM += Integer.parseInt(counts[5]);
							count_SINA += Integer.parseInt(counts[6]);
							count_OTHERS += Integer.parseInt(counts[7]);
							count_TOHSBCJT += Integer.parseInt(counts[8]);
						}
					}catch(NumberFormatException nfe){
						System.out.println("Read content Error: " + line);
					}
				}
				hasRead = true;
				br.close();
			}
			
			PrintWriter pw = new PrintWriter(counterFile);
			String count_string = count_HEXUN + "," + count_21CENTURY + "," + count_DONGCAI + "," 
								+ count_REDBOY + "," + count_HSBCJT + "," + count_HSBCJT_EDM + "," + count_SINA + "," +count_OTHERS + "," + count_TOHSBCJT;
			pw.println(count_string);
			pw.close();
			System.out.println("New Count: " + count_string);
			System.out.println("Read/Save counter File End...");
		} catch (IOException ioe) {
			if (FileNotFoundException.class.isInstance(ioe))
				System.out.println("Addtional File 'visitorcounter.txt' Does Not Exists.");
			else
				ioe.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		}
	}
	
	public synchronized static void startSaver(final String counterFile){
		if(saver_is_started) return;
		System.out.println("Start Visitor Counter saver...");
		System.out.println("file name = " + counterFile);
		new Timer().schedule(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				VisitorCounter.getInstance().saveCount(counterFile);
			}
		}, 1 * 60 * 1000, 1 * 2 * 60 * 1000);
		saver_is_started = true;
	}
	
}
